<?php

/**
 * 客户牌控制器
 */
class Controller_Customer extends Controller_Abstract
{
    function actionIndex()
    {
       
    }

    function actionList(){
    
    	$page = $this->_context->param('page',1);
    	$page_size = $this->_context->param('row',10);
    	$conditions='';
    	if($this->_context->post('key') && $this->_context->post('op') && $this->_context->post('value') ){
    		switch ($this->_context->post('op')){
    			case 'like':
    				$conditions = $this->_context->post('key').' like "%'.$this->_context->post("value").'%" ';
    				break;
    			default:
    				$conditions = $this->_context->post('key').$this->_context->post('op')."'".$this->_context->post('value')."'";	
    		}	
    	}
    	$list = Customer::find($conditions)->asArray()->limitPage($page, $page_size)->getAll();
    	
    	return json_encode($list);
    
    }
    
    function actionNew(){
    	
    	$customer = new Customer($_POST);
    	
	    try {
	    	
	    	$customer->save();
	    	if($customer->customer_id){
	    		$customer->code='CS'.substr('0000'.$customer->customer_id, -5);
	    		$customer->save();
	    	}
			return json_encode(array('success'=>true));
			
		} catch (QDB_ActiveRecord_ValidateFailedException $e) {			
			
			return json_encode(array('errorMsg' => $e->getMessage()));
			
		}
    	catch (Exception $e){
    		return json_encode(array('errorMsg'=>'系统出错,操作未能完成.'));
    	}
    }
    function actionUpdate(){
    	$id=$this->_context->post('customer_id');
    	$customer = Customer::find('customer_id=?',$id)->getOne();
    	
    	$customer->changeProps($_POST);
    	
    	try{
    		$customer->save();
    		return  json_encode(array('success'=>true));
    	}
    	catch (QDB_ActiveRecord_ValidateFailedException $e) {			
			
			return json_encode(array('errorMsg' => $e->getMessage()));
			
		}
    	catch (Exception $e){
    		return json_encode(array('errorMsg'=>'系统出错,操作未能完成.'));
    	}
    }
    
    function actionDestroy(){
    	
    	$id=$this->_context->post('id',0);
    	$row=Customer::find("customer_id=?",$id)->getOne();

    	try {
    		$row->destroy();
			return  json_encode(array('success'=>true));
    	}
    	catch (Exception $e){
				return json_encode(array('errorMsg'=>'系统出错,操作未能完成.'));
		}
    }
}

